package net.towee.client.com.app.view;

import net.towee.client.com.app.presenter.UserFeedbackPresenter.Display;
import net.towee.client.i18n.ToweeConstants;
import net.towee.client.resources.ClientResource;
import net.towee.client.widgets.ExtFlowPanel;

import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.inject.Inject;

public class UserFeedbackView extends PopupPanel implements Display {
	private ClientResource resource;
	private ToweeConstants constants;
	
	private final FlowPanel container = new FlowPanel();
	private final TextBox name = new TextBox();
	private final TextArea desc = new TextArea();
	private final Button sendButton = new Button();
	private boolean hasError;

	@Inject
	public UserFeedbackView(ToweeConstants constants, ClientResource resource) {
		super(true);
		this.constants = constants;
		this.resource = resource;
		
		// Enable glass
		setGlassEnabled(true);
		setGlassStyleName( resource.css().loadingBarGlass() );
		
		// Set popup size
		setWidth("400px");
		setHeight("400px");
		
		// Set style
		setStyleName( resource.css().feedBackPopup() );
		
		// Create form
		createForm();
	}

	private void createForm() {
		// Set container
		setWidget(container);
		
		// Set button text
		sendButton.setText( constants.leaveFeedback() );
		sendButton.getElement().removeAttribute("class");
		
		// Create form
		container.add(new Label( constants.leaveYourName() ));
		container.add(name);
		container.add(new Label( constants.leaveFeedbackMessage() ));
		container.add(desc);
		container.add(sendButton);
	}

	@Override
	public void showForm() {
		center();
	}

	@Override
	public String getUserName() {
		return name.getText().trim();
	}

	@Override
	public String getMessage() {
		return desc.getText().trim();
	}

	@Override
	public HasClickHandlers getSendButton() {
		return sendButton;
	}

	@Override
	public void noUserName() {
		this.hasError = true;
	}

	@Override
	public void noMessage() {
		this.hasError = true;
	}

	@Override
	public boolean hasErrors() {
		boolean r = hasError;
		hasError = false;
		return r;
	}

	@Override
	public void onSending() {
		sendButton.setEnabled(false);
	}

	@Override
	public void onSend() {
		Timer t = new Timer() {
			@Override
			public void run() {
				hide();	
			}
		};
		
		t.schedule(2000);
		
		Label l = new Label( constants.thanksForFeedback() );
		l.getElement().setAttribute("thx", "");
		container.clear();
		container.add( l );
	}

}
